package single_game.logic;

import java.util.ArrayList;
import java.util.Random;

import single_game.logic.race_simulator.race_team.RaceRider;
import data.Team;

public class ChooseRandomRiders {
	
	private static Random random = new Random();
	ArrayList<Team> teams;
	@SuppressWarnings("rawtypes")
	private static ArrayList[] otherTeams;
	
	@SuppressWarnings("unchecked")
	public static ArrayList<RaceRider>[] generateTeams(int myTeam, ArrayList<Team> teams, int amount) {
		
		otherTeams = new ArrayList[teams.size()-1];
		
		for (int i = 0; i < teams.size()-1; i++) {
			otherTeams[i] = new ArrayList<RaceRider>();
		}		
		
		int ptr = 0;
		for (int i = 0; i < teams.size(); i++) {
			
			if (i != myTeam) {
				otherTeams[ptr] = generateRiders(teams.get(i), amount);
				ptr++;
			}
			
		}
		
		return otherTeams;	
	}
	
	private static ArrayList<RaceRider> generateRiders(Team team, int amount) {
		
		ArrayList<Integer> tmp = team.teamRiders;
		ArrayList<RaceRider> result = new ArrayList<RaceRider>(amount);

		if (tmp.size() >= amount) {
			
			for (int i = 0; i < amount; i++) {				
				int index = random.nextInt(tmp.size());
				result.add(new RaceRider(team.getTeamId(), tmp.get(index)));
				tmp.remove(index);
			}
			
			return result;
		}
		else {
			System.out.println("ChooseRandomRiders - Team is smaller than riders amount");
			return null;
		}	
	}
}
